dnl Process this file with autoconf to produce a configure script.
dnl configure.in for gnuplot 3.7 on Unix.
dnl
dnl $Id: configure.in,v 1.41.2.7 1999/10/22 18:29:24 lhecking Exp $
dnl
AC_INIT(graphics.c)
AC_PREREQ(2.13)
AC_REVISION($Revision: 1.41.2.7 $)
VERSION=`sed -n 's/.*version.*"\(.*\)".*/\1/p' $srcdir/version.c|tr -d ' '`
PL=`sed -n 's/.*patchlevel.*"\(.*\)".*/\1/p' $srcdir/version.c|tr -d ' '`
AM_INIT_AUTOMAKE(gnuplot, $VERSION.$PL)
AC_CONFIG_HEADER(config.h:config.hin)

dnl Optional features.

dnl Use builtin readline or GNU readline
AC_ARG_WITH(readline,dnl
[  --with-readline         use the builtin minimal readline function (default)
  --without-readline      do not use the builtin readline function
  --with-readline=gnu     use the GNU readline library
  --with-readline=PATH            Specify the location of GNU readline],,
test -z "$with_readline" && with_readline=yes
)dnl

dnl If no PATH argument given, use the Unix plot library
dnl Else use either GNU libplot from plotutils,
dnl or ancient GNU graphics libplot
AC_ARG_WITH(plot,dnl
[  --with-plot             use the Unix plot library
  --with-plot=PATH                Specify the location of GNU libplot],,
test -z "$with_plot" && with_plot=no
)dnl

dnl The gif terminal with Tom Boutell's gd library
AC_ARG_WITH(gd,dnl
[  --with-gd               enable gif terminal with Tom Boutell's gd library
                          (requires GD library)
  --with-gd=PATH                  Specify the location of libgd],,
test -z "$with_gd" && with_gd=yes
)dnl

dnl The Portable Network Graphics (png) terminal
AC_ARG_WITH(png,dnl
[  --with-png              enable png terminal
                          (requires libpng and libz)
  --with-png=PATH                 Specify the location of libpng],,
test -z "$with_png" && with_png=yes
)dnl

dnl Use .gnuplot file in current directory
AC_ARG_WITH(cwdrc,dnl
[  --with-cwdrc            check current directory for .gnuplot file,
                          normally disabled for security reasons],,
test -z "$with_cwdrc" && with_cwdrc=no
)dnl

dnl Install the lasergnu printer script
AC_ARG_WITH(lasergnu,dnl
[  --with-lasergnu         install lasergnu printer script],
if test "$withval" = yes; then LASERGNU=lasergnu_install; \
else LASERGNU=lasergnu_noinstall; fi, LASERGNU=lasergnu_noinstall
)dnl

dnl The Linux console driver
AC_ARG_WITH(linux-vga,dnl
[  --with-linux-vga        use the Linux SVGA console driver
                          (requires /usr/lib/libvga)],,
test -z "$with_linux_vga" && with_linux_vga=no
)dnl

dnl IRIS terminal on IRIS4D series computer
AC_ARG_ENABLE(iris,dnl
[  --enable-iris           enable IRIS terminal (IRIS4D only)],,
test -z "$enable_iris" && enable_iris=no
)dnl

dnl MGR Window system
AC_ARG_ENABLE(mgr,dnl
[  --enable-mgr            enable MGR terminal],,
test -z "$enable_mgr" && enable_mgr=no
)dnl

dnl Redwood Graphics Interface Protocol
AC_ARG_ENABLE(rgip,dnl
[  --enable-rgip           enable Redwood Graphics Interface Protocol],,
test -z "$enable_rgip" && enable_rgip=no
)dnl

dnl configure.in body

dnl Compiler characteristics
dnl Check for ANSI C prototypes, the const and inline keywords,
dnl and ANSI style stringification
AC_PROG_CC
AM_C_PROTOTYPES
AC_PROG_CPP
AC_C_CONST
AC_C_INLINE
AC_C_STRINGIZE

AC_MSG_CHECKING(if compiler handles warn and error directives correctly)
AC_TRY_CPP([#if 0
#error "error"
#warning "warning"
#endif],
  ERRORFIX=
  AC_MSG_RESULT(yes),dnl
  ERRORFIX=errorfix
  AC_MSG_RESULT(no))
AC_SUBST(ERRORFIX)

dnl X Window System files.
AC_SUBST(X_LIBRARIES)
AC_SUBST(GNUPLOT_X11)
AC_PATH_XTRA

dnl Needed for LynxOS until AC_PATH_XTRA is fixed
if test $ac_cv_lib_nsl_gethostbyname = no; then
  AC_CHECK_LIB(bsd, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lbsd")
fi

if test "$no_x" != yes; then
  CPPFLAGS="$CPPFLAGS $X_CFLAGS"
  X_LIBRARIES="$X_PRE_LIBS $X_LIBS -lX11 $X_EXTRA_LIBS"
  GNUPLOT_X11=gnuplot_x11
  AC_DEFINE(X11, 1,
            [ Define if you are using the X11 window system. ])
fi


dnl Operating systems.
dnl FIXME AC_DEFINE(ISC22)
dnl FIXME AC_DEFINE(KSR)
dnl Check for MSDOS and djgpp, NeXT
GP_MSDOS
GP_NEXT

dnl Apparently, -lNeXT_s is needed on NeXT
dnl _instead_ of -lm ...
AC_CHECK_FUNC(sin)
if test "$ac_cv_func_sin" = no ; then
  AC_CHECK_LIB(m,sin)
fi
dnl this has to be after -lsys_s on NeXT
dnlLIBS="$LIBS -lm"


dnl Header files. ANSI first
dnl We prefer that the absense of a macro is the norm, so in syscfg.h
dnl configure's HAVE_XXXX defines are translated into NO_XXXX for ANSI
dnl headers and functions
AC_HEADER_STDC

dnl Standard headers
AC_CHECK_HEADERS(errno.h float.h limits.h locale.h math.h stdlib.h string.h \
  time.h sys/time.h sys/timeb.h sys/types.h)
dnl other
AC_CHECK_HEADERS(sys/bsdtypes.h sys/select.h sys/socket.h sys/stat.h \
  sys/systeminfo.h sys/utsname.h libc.h malloc.h sgtty.h termios.h values.h)
test "$ac_cv_header_sys_stat_h" = yes && AC_HEADER_STAT

dnl check if unistd actually declares anything. On NeXT 3.2 unistd is
dnl conditionalized for _POSIX_SOURCE
AC_MSG_CHECKING(for unistd.h)
AC_EGREP_HEADER(execv, unistd.h, AC_DEFINE(HAVE_UNISTD_H) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

dnl check if errno.h header declares errno
AC_MSG_CHECKING(if errno variable is declared)
AC_TRY_COMPILE(
[#include <stdio.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif],
  errno=0,AC_MSG_RESULT(yes),dnl
  AC_DEFINE(EXTERN_ERRNO, 1,
            [ Define if <errno.h> declares errno. ])
  AC_MSG_RESULT(no)
)dnl


dnl Types.
AC_TYPE_SIZE_T
AC_MSG_CHECKING(for time_t in time.h)
AC_EGREP_HEADER(time_t,time.h,dnl
  [ ac_cv_type_time_t=yes; AC_MSG_RESULT(yes) ],dnl
  [ AC_MSG_RESULT(no) AC_CHECK_TYPE(time_t, long)])
AC_TYPE_SIGNAL
 

dnl Functions. Standard first, then others

dnl sunos 4 has on_exit() in place of atexit()
AC_CHECK_FUNCS(atexit on_exit memcpy memmove memset bcopy bzero setvbuf \
  strerror strchr index strrchr rindex strstr)

dnl gamma is called lgamma on apollos and linux
dnl we prefer lgamma over gamma, see specfun.c
dnl math lib is already available, see operating systems part
AC_CHECK_FUNCS(erf erfc gamma lgamma getcwd pclose popen sleep snprintf \
  strncasecmp strnicmp sysinfo tcgetattr)
if test "$ac_cv_func_pclose" = yes && test "$ac_cv_func_popen" = yes ; then
  AC_DEFINE(PIPES, 1,
            [ Define if you do have the popen and pclose functions. ])
fi

dnl Argument types of select()
AC_FUNC_SELECT

dnl On SVR3.
dnl FIXME AC_DEFINE(CRIPPLED_SELECT)

dnl check for installed linux vgalib
if test "$with_linux_vga" = yes; then
  AC_MSG_CHECKING(for linux vga library)
  AC_CHECK_LIB(vga, vga_init,
    [AC_DEFINE(LINUXVGA, 1,
               [ Define if this is a Linux system with SuperVGA library. ])
     LINUXSUID='chown root $(bindir)/gnuplot; chmod u+s $(bindir)/gnuplot'
     TERMLIBS="$TERMLIBS -lvga"],dnl
    with_linux_vga=no)
fi

dnl GNU readline and the required terminal library
if test "$with_readline" != no; then
  if test "$with_readline" != yes; then
    dnl check for terminal library
    dnl this is a very cool solution from octave's configure.in
    gp_tcap=""
    for termlib in ncurses curses termcap terminfo termlib; do
      AC_CHECK_LIB(${termlib}, tputs, [gp_tcap="$gp_tcap -l$termlib"])
      case "$gp_tcap" in
        *-l${termlib}*)
          break
        ;;
      esac
    done

    if test "$with_readline" = gnu; then
      AC_CHECK_LIB(readline, readline,dnl
       TERMLIBS="$TERMLIBS -lreadline $gp_tcap",, ${gp_tcap})
    else
      GP_PATH_LIB(readline,readline,[$with_readline],[${gp_tcap}])
      if test "$ac_cv_lib_readline_readline" != no; then
        TERMLIBS="$TERMLIBS $gp_tcap"
      fi
    fi

    if test "$ac_cv_lib_readline_readline" = no; then
      AC_MSG_WARN([GNU readline not found - falling back to builtin readline])
      with_readline=yes
    else
      AC_DEFINE(HAVE_LIBREADLINE, 1,
                [ Define if you are using the GNU readline library. ])
    fi # ! ac_cv_lib_readline_readline = no

  fi # ! with_readline != yes

  if test "$with_readline" = yes; then
    AC_DEFINE(READLINE, 1,
              [ Define if you want to use the included readline function. ])
  fi

fi
dnl end readline

dnl libplot
if test "$with_plot" != no; then
  if test "$with_plot" = yes; then
    dnl Unix plot library
    AC_CHECK_LIB(plot,openpl,dnl
     AC_DEFINE(UNIXPLOT, 1,
               [ Define if you want to use the standard Unix plot library. ])
     TERMLIBS="$TERMLIBS -lplot",dnl
     with_plot=no)
  else
    dnl GNU plot library - test for new version first
    TERMXLIBS="$X_LIBS -lXaw -lXmu -lXt -lXext -lX11"
    GP_PATH_LIB(plot,pl_textangle,[$with_plot],[$TERMXLIBS])
    if test "$ac_cv_lib_plot_pl_textangle" = no; then
      GP_PATH_LIB(plot,textangle,[$with_plot],[$TERMXLIBS])
      if test "$ac_cv_lib_plot_textangle" = no; then
        TERMXLIBS=
        GP_PATH_LIB(plot,rotate,[$with_plot])
        if test "$ac_cv_lib_plot_rotate" = no; then
          AC_MSG_WARN([can't find GNU libplot -- disabling gnugraph terminal])
          with_plot=no
        else
          AC_MSG_RESULT([ - using obsolete GNU graphics library])
        fi # old GNU plotlib
      else
        AC_MSG_RESULT([ - using GNU plotutils library])
      fi # GNU plotlib pre-2.2
    else
      GP_PATH_HEADER(plotcompat.h,[$with_plot],dnl
        AC_DEFINE(HAVE_PLOTCOMPAT_H, 1,
                  [ Define if you have the <plotcompat.h> header file. ]),
        AC_MSG_WARN([found GNU plotutils 2.2+ library but not plotcompat.h])
        AC_MSG_WARN([please add path to plotcompat.h to CPPFLAGS in Makefile])
      ) # plotcompat.h
    fi # new GNU plotlib

    if test "$with_plot" != no; then
      AC_DEFINE(GNUGRAPH, 1,
                [ Define if you want to use the GNU version of the Unix plot library. ])
      AC_DEFINE(GNU_PLOTUTILS, 1,
                [ Define if your libplot.a is from GNU plotutils. ])
    fi

  fi # ! Unix plotlib
fi
dnl end libplot

dnl check for Tom Boutell's gd library
if test "$with_gd" != no; then
  GP_PATH_LIB(gd,gdImageCreate,[$with_gd])
  if test "$ac_cv_lib_gd_gdImageCreate" != no; then
    GP_PATH_HEADER(gd.h,[$with_gd],dnl
      dnl ACTION-IF-FOUND
      AC_DEFINE(HAVE_LIBGD, 1,
                [ Define if you have Thomas Boutell's gd library. ]),dnl
      dnl ACTION-IF-NOT-FOUND
      AC_MSG_WARN([found gd library but not gd.h])
      AC_MSG_WARN([please add path to gd.h to CPPFLAGS in Makefile]))
  else
    AC_MSG_RESULT([- see term/gif.trm for download details])
  fi
fi
dnl end gd

dnl check for png and z library
if test "$with_png" != no; then
  GP_PATH_LIB(png,png_info_init,[$with_png],[-lz])
  if test "$ac_cv_lib_png_png_info_init" != no; then
    TERMLIBS="$TERMLIBS -lz"
    GP_PATH_HEADER(png.h,[$with_png],,dnl
     AC_MSG_WARN([found png library but not png.h])
     AC_MSG_WARN([please add path to png.h to CPPFLAGS in Makefile])
    )
    GP_PATH_HEADER(zlib.h,[$with_png],,dnl
     AC_MSG_WARN([found z library but not zlib.h])
     AC_MSG_WARN([please add path to zlib.h to CPPFLAGS in Makefile])
    )
    if test "$ac_cv_header_png_h" != no; then
      AC_EGREP_CPP([You lose], [#include "png.h"
#if PNG_LIBPNG_VER < 89
You lose.
#endif
],dnl
       AC_MSG_WARN([- libpng too old - not including png driver])
       with_png=no)
    else
      AC_MSG_WARN([- cannot check png version - compilation may fail])
    fi

  if test "$with_png" != no; then
    AC_DEFINE(HAVE_LIBPNG, 1,
              [ Define if you have the png library. ])
  fi

  else
    AC_MSG_RESULT([- see term/png.trm for download details])
  fi
fi
dnl end png and z

dnl How do we detect the Ultrix X libraries?
dnl Or is it really a server (run-time) problem?
dnl FIXME AC_DEFINE(ULTRIX_KLUDGE)

dnl Removed enable options for these
dnl Just autodetect

dnl SCO cgi
AC_EGREP_CPP(yes,
[#ifdef SCO
yes
#endif
],AC_CHECK_LIB(ccgi, v_opnwk,dnl
    AC_DEFINE(CGI, 1,
              [ Define if you want to use the CGI terminal under SCO. ])dnl
              TERMLIBS="$TERMLIBS -lccgi"))

dnl Sun sunview
AC_EGREP_CPP(yes,
[#ifdef sun
yes
#endif
], AC_CHECK_LIB(suntool, window_create,dnl
    AC_CHECK_HEADER(suntool/sunview.h,dnl
    AC_DEFINE(SUN, 1,
              [ Define if you want to use the sunview terminal (sun). ])dnl
    TERMLIBS="$TERMLIBS -lsuntool -lsunwindow -lpixrect"),,dnl
    [-lsunwindow -lpixrect]))

dnl Process with and enable options

if test "$with_cdwrc" = no; then
  AC_DEFINE(NOCWDRC, 1,
            [ Define if you do not want to read .gnuplot in the current directory. ])
fi

if test "$enable_iris" = yes; then
  AC_CHECK_LIB(gl_s,winopen,dnl
   AC_DEFINE(IRIS, 1,
             [ Define if you want to use the IRIS terminal on IRIS4D series computers. ])
   TERMLIBS="$TERMLIBS -lgl_s",dnl
   enable_iris=no)
fi

if test "$enable_mgr" = yes; then
  AC_CHECK_LIB(mgr,m_getinfo,dnl
   AC_DEFINE(MGR, 1,
             [ Define if you want to use the MGR Window system. ])
   TERMLIBS="$TERMLIBS -lmgr -lpixrect",dnl
   enable_mgr=no,[-lpixrect])
fi

if test "$enable_rgip" = yes; then
  AC_DEFINE(RGIP, 1,
            [ Define if you want to use the Redwood Graphics Interface Protocol. ])
fi

dnl Substitute variables
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)
AC_SUBST(LASERGNU)
AC_SUBST(LINUXSUID)
AC_SUBST(TERMLIBS)
AC_SUBST(TERMXLIBS)


dnl Report configuration
eval gp_datadir=$datadir
test $gp_datadir = NONE/share && gp_datadir=/usr/local/share
AC_MSG_RESULT([
** Configuration summary for $PACKAGE $VERSION:

  Where is the help file?           $gp_datadir/gnuplot.gih
])
test "$ac_cv_header_gd_h" = yes && \
  AC_MSG_RESULT([  Enable generation of GIF files])

test "$ac_cv_header_zlib_h" = yes && \
  AC_MSG_RESULT([  Enable generation of PNG files])

test "$with_cwdrc" = yes && \
  AC_MSG_RESULT([  Check current directory for .gnuplot file])

if test "$with_readline" != no; then
  if test "$with_readline" != yes; then
    AC_MSG_RESULT([  Use GNU readline library])
      test -n "$gp_tcap" && AC_MSG_RESULT([    with ${gp_tcap}])
  else
    AC_MSG_RESULT([  Use builtin minimal readline])
  fi
fi

if test "$with_plot" = yes; then
  AC_MSG_RESULT([  Use the Unix plot library])
elif test "$with_plot" != no; then
  AC_MSG_RESULT([  Use the GNU plot library,])
  if test x"$ac_cv_header_plotcompat_h" != x && \
     test "$ac_cv_header_plotcompat_h" != no ; then
    AC_MSG_RESULT([    version 2.2 or newer])
  else
    AC_MSG_RESULT([    version 2.1 or older])
  fi
fi

test "$with_lasergnu" = yes && \
  AC_MSG_RESULT([  Install lasergnu printer script])

if test "$with_linux_vga" = yes; then
  AC_MSG_RESULT([  Use the Linux SVGA console driver])
  AC_MSG_RESULT([    SECURITY NOTICE: SVGAlib requires that])
  AC_MSG_RESULT([    gnuplot is installed suid root!])
fi

test "$enable_iris" = yes && \
  AC_MSG_RESULT([  Enable IRIS terminal])

test "$enable_mgr" = yes && \
  AC_MSG_RESULT([  Enable MGR terminal])

test "$enable_rgip" = yes && \
  AC_MSG_RESULT([  Enable RGIP terminal])

test "$no_x" != yes && \
  AC_MSG_RESULT([  Use the X Window System
])
dnl end config report

dnl Write Makefiles and configuration header
AC_OUTPUT([Makefile docs/Makefile docs/latextut/Makefile], \
[test -z "$CONFIG_HEADERS" || echo timestamp >stamp-h])

dnl end configure.in
